package com.tsing.common.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class TestZip {

    public static final String file = System.getProperty("user.dir") + File.separator + "log/sys.log";

    public static void main(String[] args) {
        List<String> list = new ArrayList();
        String[] strs = new String[5];
        StringBuffer sb = new StringBuffer();
        try {
            sb.append(file);
            writeZip(sb, "newZipFile");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void writeZip(StringBuffer sb, String zipname) throws IOException {
        String[] files = sb.toString().split(",");
        System.out.println(files);
        OutputStream os = new BufferedOutputStream(new FileOutputStream(zipname + ".zip"));
        ZipOutputStream zos = new ZipOutputStream(os);
        byte[] buf = new byte[8192];
        int len;
        for (int i = 0; i < files.length; i++) {
            File file = new File(files[i]);
            if (!file.isFile()) continue;
            ZipEntry ze = new ZipEntry(file.getName());
            zos.putNextEntry(ze);
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            while ((len = bis.read(buf)) > 0) {
                zos.write(buf, 0, len);
            }
            zos.closeEntry();
        }
//        zos.setEncoding("UTF-8");
        zos.closeEntry();
        zos.close();
    }

}